//
//  DateViewController.m
//  Days
//
//  Created by lanou3g on 16/1/6.
//  Copyright © 2016年 dss. All rights reserved.
//

#import "DateViewController.h"
#import "CKCalendarView.h"
@interface DateViewController ()

{
    BOOL tag;
}

@end

@implementation DateViewController

- (id)init {
    self = [super init];
    if (self) {
        //        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"日历背景.jpg"]];
        //        [self.view addSubview:imageView];
        
        CKCalendarView *calendar = [[CKCalendarView alloc] initWithStartDay:startMonday];
        calendar.frame = CGRectMake(self.view.frame.size.width-550, self.view.frame.size.height-200, 270, 200);
        [self.view addSubview:calendar];
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.beginDateLabel.text = [self dateToStringDate:[NSDate date]];
    self.endDateLabel.text = [self dateToStringDate:[NSDate date]];
    self.datePicker.hidden = YES;
    self.barButtonItem.hidden = YES;
    
    _allSecondDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)];
    _allSecondDataLabel.hidden = YES;
    _allSecondDataLabel.userInteractionEnabled = YES;
    _allSecondDataLabel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_allSecondDataLabel];
    
    //轻拍手势
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRAction)];
    _secondDataLabel.userInteractionEnabled = YES;
  [_secondDataLabel addGestureRecognizer:tapGR];
 
    tag = YES;
}

- (void)tapGRAction{
    if (tag == YES) {
        tag = NO;
        _allSecondDataLabel.text = self.secondDataLabel.text;
        _allSecondDataLabel.hidden = NO;
    }
    else{
        tag = YES;
    _allSecondDataLabel.hidden = YES;
    
    }
}



//开始时间
- (IBAction)beginTimeControl:(id)sender {
    
    if (!self.beginDateButton.selected) {
        self.beginDateButton.selected = YES;
        self.datePicker.hidden = NO;
        self.datePicker.date = [self dateFromString:self.beginDateLabel.text];
        
         self.barButtonItem.hidden = NO;
    }else{
        [self deletedatePicker:nil];
    }

}

//结束时间
- (IBAction)endTimeControl:(id)sender {
    
    if (!self.endDateButton.selected) {
        self.endDateButton.selected = YES;
        self.datePicker.hidden = NO;
        self.datePicker.date = [self dateFromString:self.endDateLabel.text];
        self.barButtonItem.hidden = NO;
    }else{
        [self deletedatePicker:nil];
    }

    
}

//dataPicker
- (IBAction)deletedatePicker:(id)sender {
    
//    if (self.endDateButton.selected) {
//        self.endDateLabel.text = [self dateToStringDate:self.datePicker.date];
//        self.endDateButton.selected = NO;
//    }
//    if (self.beginDateButton.selected) {
//        self.beginDateLabel.text = [self dateToStringDate:self.datePicker.date];
//        self.beginDateButton.selected = NO;
//    }
    
  //  self.datePicker.hidden = YES;
   // tooBar.hidden = YES;
    
    
}

//确定
- (IBAction)determineButton:(id)sender {
    
        if (self.endDateButton.selected) {
            self.endDateLabel.text = [self dateToStringDate:self.datePicker.date];
            self.endDateButton.selected = NO;
            self.datePicker.hidden = YES;
            self.barButtonItem.hidden = YES;
        }
        if (self.beginDateButton.selected) {
            self.beginDateLabel.text = [self dateToStringDate:self.datePicker.date];
            self.beginDateButton.selected = NO;
        }
    
    
}





//计算天数
- (IBAction)countDay:(id)sender {
    
    NSDate *beginDate = [self dateFromString:self.beginDateLabel.text];
    NSDate *endDate = [self dateFromString:self.endDateLabel.text];
    NSDate *earlyDate = [beginDate earlierDate:endDate];
    
    if ([earlyDate isEqualToDate:endDate] && ![earlyDate isEqualToDate:beginDate]) {
        
        [self alterMessage:@"开始时间不得晚于结束时间"];
        return;
}
    
        
        NSTimeInterval time = [endDate timeIntervalSinceDate:beginDate];
        
    
        float allDay = time / (24 * 60 * 60);
        float seconds = time ;
    float hours = time / 3600;
    
        self.computationDayLabel.text = [NSString stringWithFormat:@"%d 天",(int)allDay];
        self.secondDataLabel.text = [NSString stringWithFormat:@"%d S",(int)seconds];
        self.hoursDataLabel.text = [NSString stringWithFormat:@"%d H",(int)hours];
    }


- (void)alterMessage:(NSString *)messageString{
    
    UIAlertView *al = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:messageString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [al show];
}

#pragma mark- ----------ComputationTime

- (NSString *)dateToStringDate:(NSDate *)Date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
    NSString *destDateString = [dateFormatter stringFromDate:Date];
    destDateString = [destDateString substringToIndex:10];
    
    return destDateString;
}

- (NSDate *)dateFromString:(NSString *)dateString{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date1=[formatter dateFromString:dateString];
    
    NSDate *newDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:([date1 timeIntervalSinceReferenceDate] + 8*3600)];
    return newDate;
}





@end
